package cn.hist.hfp.util;

import cn.hist.hfp.exception.ErrorCode;
import cn.hist.hfp.exception.InternalException;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class Encryption {

    public String encodeByMd5(String str){
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new InternalException(ErrorCode.NO_SUCH_ALGORITHM);
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(digest.digest(str.getBytes(StandardCharsets.UTF_8)));
    }
}
